package cn.cdyx.ydhl.config;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableOpenApi
public class SwaggerYDHLConfig {
    @Value("${swagger.host:127.0.0.1:8080}")
    private String swaggerHost;
    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        // 指定扫描包路径
        return new Docket(DocumentationType.SWAGGER_2) // 指定生成的文档的类型是Swagger2
                //地址
                .host(swaggerHost)
                .apiInfo(apiInfo())
                // 用来创建该API的基本信息，展示在文档的页面中（自定义展示的信息）,配置文档页面的基本信息内容
                // 设置哪些接口暴露给Swagger展示
                .select()

                // 扫描所有有注解的api，用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                // 扫描所有 .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build()
               // .globalRequestParameters(getGlobleRequestParameters())
                .globalResponses(HttpMethod.GET,getGlobalRsponseMessage())
                .globalResponses(HttpMethod.POST,getGlobalRsponseMessage());
    }
    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("移动护理接口文档")
                // 描述
                .description("护理接口")
                //作者信息、联系方式：Contact(String name, String url, String email)
                .contact(new Contact("hxy","https://www.baidu.com","hxy@cdyx.com"))
                // 版本
                .version("1.0.0")
                .build();
    }

    private List<RequestParameter> getGlobleRequestParameters(){
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name("uuid")
                .description("设备uuid")
                .required(true)
                .in(ParameterType.QUERY)
                .query(q->q.model(m->m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build());
        return parameters;
    }

    private List<Response> getGlobalRsponseMessage(){
        List<Response> responses = new ArrayList<>();
        responses.add(new ResponseBuilder().code("404")
                .description("未找到资源")
                .build());
        responses.add(new ResponseBuilder().code("5999")
                .description("接口内部错误")
                .build());
        responses.add(new ResponseBuilder().code("1004")
                .description("查询错误")
                .build());
        return responses;
    }

}
